package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message is used by the application to react to an incoming call.
 * The incoming call is identified by the parameter PLCI. The parameter
 * Reject is used to accept, reject or ignore the call.
 *
 * The absence (i.e. coding as an empty structure) of the parameter B
 * protocol results in the default protocol behavior: ISO 7776 (X.75)
 * and window size seven. This is a recommended selection to obtain
 * general connectivity with recovery. Note that ISO 7776 describes a
 * default maximum whereas COMMON-ISDN-API is able to handle up to
 * on the CAPI_REGISTER values of an application.
 */
public class ConnectResp
        extends CapiActiveMessage
        implements PlciProperty {

    /** The PLCI. */
    private int _plci;

    /** Accept or reject the call. */
    private Reject _reject;

    /** Selection of B protocols. */
    private BProtocol _bProtocol = new BProtocol();

    /** The connected number. */
    private ConnectedNumber _connectedNumber = new ConnectedNumber();

    /** The connected subaddress. */
    private SubAddress _connectedSubAddress = new SubAddress();

    private Object _llc;
    private Object _additionalInfo;

    /**
     * Create a new {@code ConnectResp} message with the specified PLCI.
     *
     * @param plci The PLCI.
     */
    public ConnectResp(final int plci) {
        super(Command.CONNECT_RESP);
        setPlci(plci);
    }

    /**
     * Create a new {@code ConnectResp} message with the PLCI taken from the
     * specified {@code ConnectInd} message.
     *
     * @see #ConnectResp(int plci)
     * @param connectInd The {@code ConnectInd} which contains a valid PLCI.
     */
    public ConnectResp(final ConnectInd connectInd) {
        this(connectInd.getPlci());
    }

    /** {@inheritDoc} */
    public int getPlci() {
        return _plci;
    }

    /** {@inheritDoc} */
    public void setPlci(final int plci) {
        _plci = plci;
    }

    /**
     * Retrieve the connected number.
     *
     * @return The connected number.
     */
    public ConnectedNumber getConnectedNumber() {
        return _connectedNumber;
    }

    /**
     *
     * @param connectedNumber
     */
    public void setConnectedNumber(final ConnectedNumber connectedNumber) {
        _connectedNumber = connectedNumber;
    }

    /**
     *
     * @return
     */
    public SubAddress getConnectedSubAddress() {
        return _connectedSubAddress;
    }

    /**
     *
     * @param connectedSubAddress
     */
    public void setConnectedSubAddress(final SubAddress connectedSubAddress) {
        _connectedSubAddress = connectedSubAddress;
    }

    /**
     *
     * @return
     */
    public Reject getReject() {
        return _reject;
    }

    /**
     * Set the Reject parameter which determines whether a call is rejected
     * or accepted.
     *
     * @param reject
     */
    public void setReject(final Reject reject) {
        _reject = reject;
    }

    public BProtocol getBProtocol() {
        return _bProtocol;
    }

    /** {@inheritDoc} */
    protected void serializeTo(final ByteBuffer byteBuffer) {
        byteBuffer.putInt(_plci);
        _reject.writeTo(byteBuffer);
        _bProtocol.writeTo(byteBuffer);
        _connectedNumber.writeTo(byteBuffer);
        _connectedSubAddress.writeTo(byteBuffer);
        byteBuffer.put((byte) 0); // TODO: LLC
        byteBuffer.put((byte) 0); // TODO: Additional Info
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format(
                "%s[%s plci=0x%08x reject=%s bProt=%s connected#=%s "
                + "connectedSub#=%s llc=%s addInfo=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getPlci(),
                getReject(),
                getBProtocol(),
                getConnectedNumber(),
                getConnectedSubAddress(),
                _llc,
                _additionalInfo);

        return s;
    }

}
